
jQuery(document).ready(function($) {
    'use strict';

    // ============================================================== 
    // Notification list
    // ============================================================== 
    if ($(".notification-list").length) {

        $('.notification-list').slimScroll({
            height: '250px'
        });

    }

    // ============================================================== 
    // Menu Slim Scroll List
    // ============================================================== 


    if ($(".menu-list").length) {
        $('.menu-list').slimScroll({

        });
    }

    // ============================================================== 
    // Sidebar scrollnavigation 
    // ============================================================== 

    if ($(".sidebar-nav-fixed a").length) {
        $('.sidebar-nav-fixed a')
            // Remove links that don't actually link to anything

            .click(function(event) {
                // On-page links
                if (
                    location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') &&
                    location.hostname == this.hostname
                ) {
                    // Figure out element to scroll to
                    var target = $(this.hash);
                    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                    // Does a scroll target exist?
                    if (target.length) {
                        // Only prevent default if animation is actually gonna happen
                        event.preventDefault();
                        $('html, body').animate({
                            scrollTop: target.offset().top - 90
                        }, 1000, function() {
                            // Callback after animation
                            // Must change focus!
                            var $target = $(target);
                            $target.focus();
                            if ($target.is(":focus")) { // Checking if the target was focused
                                return false;
                            } else {
                                $target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
                                $target.focus(); // Set focus again
                            };
                        });
                    }
                };
                $('.sidebar-nav-fixed a').each(function() {
                    $(this).removeClass('active');
                })
                $(this).addClass('active');
            });

    }

    // ============================================================== 
    // tooltip
    // ============================================================== 
    if ($('[data-toggle="tooltip"]').length) {
            
            $('[data-toggle="tooltip"]').tooltip()

        }

     // ============================================================== 
    // popover
    // ============================================================== 
       if ($('[data-toggle="popover"]').length) {
            $('[data-toggle="popover"]').popover()

    }
     // ============================================================== 
    // Chat List Slim Scroll
    // ============================================================== 
        

        if ($('.chat-list').length) {
            $('.chat-list').slimScroll({
            color: 'false',
            width: '100%'


        });
    }

});

function load_content(url) {
    clearInterval(instantRefresh);
    $.ajax({
    url: url,
    type: "GET",
    success: function (data) {
      if (data) {
      $("#content").html(data);
      }
    },
  });
}

function search() {
    event.preventDefault();
    let q = $('#q').val();
    $.ajax({
        url: "/software",
        type: "GET",
        data: {'q': q},
        success: function (data) {
            if (data) {
                $("#content").html(data);
            }
        }
    });
}

function getSystemInformation() {
    let cpu=$("#cpu");
    let memory=$("#memory");
    let memoryUsed=$("#memory-used");
    let cpuUsed=$("#cpu-used");
    $.ajax({
        url: "/api/v1/system_status",
        type: "GET",
        success: function (data) {
            cpu.html('');
            memory.html('');
            Morris.Donut({
                element: 'memory',
                data: [
                    { value: data.Memory.percent, label: '内存利用率' },
                    { value: (100-data.Memory.percent), label: '空闲内存' },
                ],
                labelColor: '#2e2f39',
                gridTextSize: '14px',
                colors: ["#ff407b", "#5969ff"],
                formatter: function(x) { return x + "%"},
                resize: true
            });
            Morris.Donut({
                element: 'cpu',
                data: [
                    { value: data.CPU.percent, label: 'CPU利用率' },
                    { value: 100-data.CPU.percent, label: '空闲CPU' },
                ],
                labelColor: '#2e2f39',
                gridTextSize: '14px',
                colors: ["#ff407b", "#5969ff"],
                formatter: function(x) { return x + "%" },
                resize: true
            });
            memoryUsed.html(data.Memory.used +' / ' + data.Memory.total);
            cpuUsed.html(data.CPU.count+'核');
        }
    });
}


